home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 426-450 / disk_432 / reader / reader.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  12KB  |  352 lines

  1.                 /*==================================*/
  2.                 /*                                  */
  3.                 /*          READER  V1.0            */
  4.                 /*                                  */
  5.                 /*       © 3iff.  6 Jan 1991        */
  6.                 /*                                  */
  7.                 /*==================================*/
  8.  
  9.  
  10. #include <stdio.h>
  11. #define tolower(c) ((c) | 0x20)
  12.  
  13. short spare = 0, sp = 0;                           /* Variables */
  14. char a[20], letters[20], saveletters[20], temp[20];
  15. short wl = 0, letlen=0, flag = 0, matcher = 0;     /* matcher on for test!! */
  16. short limit = 99, count = 0; newfile = 0;          /* no limit! */
  17. int cmatch = 0;
  18. short k[27] [12];
  19. float percent;
  20.  
  21. FILE *input, *output, *fopen();              /* file declarations */
  22.  
  23.  
  24. matchword()                                  /* match a word pattern */
  25.                                              /* available letters    */
  26. {
  27. register char *atext = a, *alet = letters;   /* Set vars & pointers */
  28. short wpos = 0, flag = 0;
  29.  
  30. while ((*atext) && (flag == 0))              /* till the end of word or.. */
  31.   {
  32.   if (letlen > (wl - wpos))                  /* word not long enough */
  33.     flag = 1;
  34.     else
  35.     {
  36.     alet = &letters[0];                        /* reset to first char */
  37.  
  38.     while((*atext != *alet) && (flag == 0) && (*alet != '.')) /* Find a matching letter */
  39.       {
  40.       atext++;                                 /* inc word pointers */
  41.       wpos++;
  42.       if ((letlen > (wl - wpos)) || (!*atext)) /* word not long enough */
  43.         {
  44.         flag = 1;                              /* mark word as failed */
  45.         }
  46.       }
  47.       if (flag != 0)                           /* word failed */
  48.         return(0);
  49.  
  50.       while(*alet++)                           /* for all the letters */
  51.         {
  52.         if(!*alet)                             /* if end of letters? */
  53.           flag = 1;                            /* word fails */
  54.           else
  55.           {
  56.           atext++;                                 /* next word char */
  57.           if((*alet != *atext) && (*alet != '.'))  /* if no match and not a spare */
  58.             {
  59.             wpos++;                                /* reset scan positions */
  60.             atext = &a[wpos];
  61.             alet = &letters[0];
  62.             goto label1;                           /* sorry about this ! */
  63.             }
  64.           }
  65.         }
  66.       flag = 2;                                    /* valid word */
  67.     }
  68.   label1:                                          /* 'goto' label */
  69.   ;
  70.   }
  71.  
  72.   if(flag == 2)                                   /* did word fail? */
  73.     recordvalid();                           /* no, record fact */
  74.  
  75. }
  76.  
  77.  
  78. checkword()                                  /* match a word to      */
  79.                                              /* available letters    */
  80. {
  81. register char *atext = a, *alet = letters;   /* Set vars & pointers */
  82.  
  83. while (*atext)                               /* till the end of word */
  84.   {
  85.   alet = &letters[0];                        /* reset to first char */
  86.  
  87.   while((*atext != *alet) && (flag == 0))    /* Find a matching letter */
  88.     {
  89.     alet++;
  90.  
  91.     if(!*alet)                               /* end of letters? */
  92.       {
  93.       if (sp >= 1)                           /* can we use a spare */
  94.         {
  95.         sp--;
  96.         flag = 1;                            /* spare used */
  97.         }
  98.         else
  99.         return(0);                           /* no spares, word fails */
  100.       }
  101.     }
  102.     if(flag == 0)                            /* did we use a spare? */
  103.       *alet = '+';                           /* no, kill used letter */
  104.       else
  105.       flag = 0;                              /* yes, reset flag */
  106.     atext++;                                 /* move to next word char */
  107.   }
  108.   recordvalid();
  109.  
  110. }
  111.  
  112.  
  113. recordvalid()                         /* record finding a valid word */
  114.  
  115. {
  116. register short j = 0, h = 0;
  117.  
  118.   printf("%-15s", a);                      /* Valid word, print it */
  119.   fprintf(output, "%-15s", a);
  120.   cmatch++;                                /* 1 more word found */
  121.   if ((cmatch % 5) == 0)                   /* end of line? */
  122.     {
  123.     puts("");                             /* yes, print new line */
  124.     fprintf(output, "\n");
  125.     }
  126.  
  127.   j = strlen(a);                           /* length of word */
  128.   if (j > 10)                              /* max length is 10 */
  129.     j = 10;
  130.   h = a[0] - 64;                           /* get initial letter */
  131.  
  132.   k[h] [j]++;                              /* init letter - count */
  133.   k[h] [0]++;                              /* total of init letter */
  134.   k[0] [j]++;                              /* total of length */
  135.   return(0);
  136. }
  137.  
  138.  
  139. void countspares()                           /* scan letter list */
  140.  
  141. {
  142. register char *tt = letters;                 /* pointer to letters */
  143. register short j = 0;
  144.  
  145. while(*tt)                                   /* scan the letters */
  146.   {
  147.   if ((!isalpha(*tt)) && (!matcher))         /* is char a real letter */
  148.     {
  149.     if ((*tt) == '.')                        /* only . is a spare */
  150.       {
  151.       spare++;                               /* no, it's a spare */
  152.       puts("Spare located.");
  153.       }
  154.     }
  155.     else
  156.     {
  157.       temp[j] = toupper(*tt);                /* a letter, make it Ucase */
  158.       j++;
  159.     }
  160.     tt++;                                    /* next letter please */
  161.   }
  162.   strcpy(letters, temp);                     /* refresh letter list */
  163.   if (!matcher)                              /* format output */
  164.     {
  165.     printf("Scanning %s ", letters);
  166.     fprintf(output, "Scanning %s ", letters);
  167.     printf("with %d spares.\n\n", spare);
  168.     fprintf(output, "with %d spares.\n\n", spare);
  169.     }
  170.     else
  171.     {
  172.     printf("Searching for %s ", letters);
  173.     fprintf(output, "Searching for %s ", letters);
  174.     puts("\n\n");
  175.     fprintf(output, "\n\n");
  176.     }
  177. }
  178.  
  179.  
  180. void datatable()
  181.  
  182. {
  183. register short j = 0, h = 0;
  184. char bar[80];
  185.  
  186.   strcpy(bar, " ------------------------------------------------------------------------");
  187.  
  188.   fprintf(output, " Letter | Total   |     4     5     6     7     8     9    10+   Percent\n");
  189.   fprintf(output,"%s\n", bar);
  190.  
  191.     for(j = 1; j <=26; j++)                      /* for A to Z */
  192.     {
  193.       if((k[j] [0] > 0) && (k[j] [0]<9999))      /* if a letter > 0 */
  194.       {
  195.         fprintf(output, "    %c   | %4d    |", 'A' + j - 1, k[j] [0]);
  196.  
  197.         for(h = 4; h <=10; h++)                  /* report word lengths */
  198.         {
  199.           if((k[j] [h] > 0) && (k[j] [h]<9999))  /* if a score > 0 */
  200.             fprintf(output, "%6d", k[j] [h]);    /* print score */
  201.             else
  202.             fprintf(output, "     -");           /* or use blanks */
  203.         }
  204.         percent = (float) (100.0 * k[j] [0] / cmatch);   /* calc % found */
  205.         fprintf(output, "  |  %5.2f%%", percent);
  206.         fprintf(output, "\n");
  207.       }
  208.     }
  209.  
  210.     fprintf(output,"%s\n", bar);
  211.     fprintf(output," Totals   %4d    |", cmatch);
  212.  
  213.     for(h = 4; h <=10; h++)                     /* report totals */
  214.     {
  215.       if((k[0] [h] > 0) && (k[0] [h]<9999))     /* if a score > 0 */
  216.         fprintf(output, "%6d", k[0] [h]);       /* print score */
  217.         else
  218.         fprintf(output, "     -");              /* or use blanks */
  219.     }
  220.     fprintf(output, "\n Percent          |");
  221.  
  222.     for(h = 4; h <=10; h++)                     /* report Percents */
  223.     {
  224.       if((k[0] [h] > 0) && (k[0] [h]<9999))     /* if a score > 0 */
  225.         {
  226.         percent = (float) (100.0 * k[0] [h] / cmatch);   /* calc % found */
  227.         fprintf(output, " %5.1f", percent);              /* print score */
  228.         }
  229.         else
  230.         fprintf(output, "     -");                       /* or use blanks */
  231.     }
  232.     fprintf(output, "\n\n\n\n");
  233.  
  234. }
  235.  
  236.  
  237. void main(argc, argv)
  238. int argc;
  239. char *argv[];
  240.  
  241. {
  242.     register char c;
  243.  
  244.     while (--argc && (*++argv)[0] == '-')    /* find a - switch */
  245.         while (c = *++argv[0])
  246.             switch (tolower(c))              /* ensure lower case */
  247.             {
  248.                case '4':                     /* set limit values */
  249.                limit = 4;
  250.                break;
  251.  
  252.                case '5':
  253.                limit = 5;
  254.                break;
  255.  
  256.                case '6':
  257.                limit = 6;
  258.                break;
  259.  
  260.                case '7':
  261.                limit = 7;
  262.                break;
  263.  
  264.                case '8':
  265.                limit = 8;
  266.                break;
  267.  
  268.                case '9':
  269.                limit = 9;
  270.                break;
  271.  
  272.                case 'l':
  273.                limit = 10;
  274.                break;
  275.  
  276.                case 'm':
  277.                matcher = 1;                        /* Matcher on */
  278.                break;
  279.  
  280.                case 'z':
  281.                newfile = 1;                        /* erase old valid */
  282.                break;
  283.  
  284.                default:
  285.                printf("\nError: unknown flag\n");   /* unknown switch */
  286.                break;
  287.              }
  288.   if (argc != 1 || **argv == '?')         /* did we get a letter list? */
  289.     puts("USAGE:  READER [-m] [-4 to -9/-l] [-z] letterlist\n");  /* how to use it */
  290.     else
  291.     {
  292.     input = fopen("ram:wordall.dat", "r");           /* open the data file */
  293.     if (!input)                                /* not found in ram: */
  294.       {
  295.       input = fopen("wordlist:wordall.dat", "r");  /* look on disk */
  296.       if (!input)                                    /* abort if not found */
  297.         {
  298.         printf("\nData file not found!!\n\n");       /* say so */
  299.         return(0);
  300.         }
  301.       }
  302.     if (newfile == 1)
  303.       output = fopen("Vld:valid", "w");        /* open the output file */
  304.       else
  305.       output = fopen("Vld:valid", "a");        /* append the output file */
  306.  
  307.     printf("\n\33[33mREADER V1.0 ©3iff 6/91.\33[31m \n\n"); /* Title */
  308.     fprintf(output, " READER V1.0 ©3iff 6/91.\n\n");
  309.     strcpy(letters, *argv);                  /* Store letters */
  310.     letlen = strlen(letters);                /* how many letters? */
  311.     countspares();                           /* search for spares */
  312.  
  313.     strcpy(saveletters, letters);            /* save the clean list */
  314.  
  315.     do
  316.       {
  317.       strcpy(a, '\0');                       /* clear the string */
  318.       fscanf(input, "%20s", a);              /* and read a word */
  319.       wl = strlen(a);                        /* how long is it? */
  320.       if (wl != 0)                           /* any characters in it? */
  321.         {
  322.         if(((limit <= 9) && (wl == limit)) || ((limit == 10) && (wl >= 10)) || limit == 99)
  323.           {
  324.           count++;                           /* count as a word checked */
  325.           sp = spare;                        /* refresh spares count */
  326.           if (matcher == 1)
  327.             matchword();                     /* do match check */
  328.             else
  329.             checkword();                     /* do check make */
  330.  
  331.           strcpy(letters, saveletters);      /* restore letter list */
  332.           }
  333.         }
  334.       } while ( wl != 0 );                   /* till the end if the file */
  335.  
  336.     percent = (float) (100.0 * cmatch / count);      /* calc % found */
  337.  
  338.     printf("\n\n   %d valid words found", cmatch); /* final report */
  339.     printf(" of %d words checked.  %5.2f%% success rate.\n ", count, percent);
  340.     fprintf(output, "\n\n   %d Valid words found", cmatch);
  341.     fprintf(output, " of %d words checked.  %5.2f%% success rate.\n\n", count, percent);
  342.  
  343.     datatable();                             /* output data table */
  344.  
  345.     fclose(input);                           /* close the files */
  346.     fclose(output);
  347.     }
  348.  
  349. }
  350.  
  351.  
  352.